package org.summerframework.component.note.inform.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.summerframework.component.mybatisplus.AbstractBasePlusService;
import org.summerframework.component.note.inform.model.dao.NoteInformRecordDAO;
import org.summerframework.component.note.inform.model.domain.NoteInformRecordDO;
import org.summerframework.component.note.inform.service.NoteInformRecordService;
import org.summerframework.component.note.inform.service.dto.NoteInformRecordDTO;
import org.summerframework.component.note.inform.service.dto.NoteInformRecordQueryDTO;

import java.time.LocalDate;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service("noteInformRecordService")
public class NoteInformRecordServiceImpl extends AbstractBasePlusService<NoteInformRecordDAO, NoteInformRecordDTO, NoteInformRecordQueryDTO, NoteInformRecordDO> implements NoteInformRecordService {

    @Override
    public Wrapper<NoteInformRecordDO> getWrapper(NoteInformRecordQueryDTO query) {
        return Wrappers.<NoteInformRecordDO>query()
                .eq(Objects.nonNull(query.getCreateTime()), "date(create_time)", query.getCreateTime())
                .lambda()
                .orderBy(Objects.nonNull(query.getOrderByCreateTime()), query.getOrderByCreateTime() != null ? query.getOrderByCreateTime() : false, NoteInformRecordDO::getCreateTime)
                .eq(StringUtils.isNotBlank(query.getIp()), NoteInformRecordDO::getIp, query.getIp())
                .orderBy(Objects.nonNull(query.getOrderById()), query.getOrderById() != null ? query.getOrderById() : false, NoteInformRecordDO::getId)
                .eq(Objects.nonNull(query.getTemplateId()), NoteInformRecordDO::getTemplateId, query.getTemplateId())
                .eq(StringUtils.isNotBlank(query.getAccount()), NoteInformRecordDO::getAccount, query.getAccount());
    }

    @Override
    public Integer selectCountTodayByIp(String ip) {
        return selectCountByQuery(NoteInformRecordQueryDTO.builder().createTime(LocalDate.now()).ip(ip).build());
    }

    @Override
    public Integer[] selectCountByAccount(String account) {
        return baseDAO.selectCountByAccount(account);
    }
}
